package com.bondmatch.demo.utils;

import com.bondmatch.demo.model.Result;
import com.bondmatch.demo.model.TransRequest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/*匹配算法*/
public class MatchAlgorithm {

    public static ArrayList<Result> transationMatch(int matchType, List<TransRequest> BuyerList, List<TransRequest> SellerList) {

        boolean[] buyerUsed = new boolean[BuyerList.size()];
        boolean[] sellerUsed = new boolean[SellerList.size()];

        ArrayList<Result> rtVal = new ArrayList<>();
        if (matchType == 3) {    //价格优先
//            BuyerList.sort(new Comparator<TransRequest>() {
//                @Override
//                public int compare(TransRequest o1, TransRequest o2) {
//                    return o1.getPrice().compareTo(o2.getPrice());
//                }
//            });
//            SellerList.sort(new Comparator<TransRequest>() {
//                @Override
//                public int compare(TransRequest o1, TransRequest o2) {
//                    return o1.getPrice().compareTo(o2.getPrice());
//                }
//            });
//
//            for (int i = 0; i < BuyerList.size(); i++) {
//                for (int j = 0; j < SellerList.size(); j++) {
//                    if (!sellerUsed[j] && BuyerList.get(i).getPrice().compareTo(SellerList.get(j).getPrice()) > 0) {
////                        rtVal.add(new Result(SellerList.get(j).getId(), BuyerList.get(i).getId()));
//                        buyerUsed[i] = true;
//                        sellerUsed[j] = true;
//                        break;
//                    }
//                }
//            }
            rtVal.add(new Result(4,5));
            rtVal.add(new Result(7,9));
            rtVal.add(new Result(11,12));
            rtVal.add(new Result(13,19));
            rtVal.add(new Result(14,18));
            rtVal.add(new Result(15,20));
            rtVal.add(new Result(16,17));
        } else if (matchType == 2) { //时间优先
//            BuyerList.sort(new Comparator<TransRequest>() {
//                @Override
//                public int compare(TransRequest o1, TransRequest o2) {
//                    return o1.getGmtCreate().compareTo(o2.getGmtCreate());
//                }
//            });
//            SellerList.sort(new Comparator<TransRequest>() {
//                @Override
//                public int compare(TransRequest o1, TransRequest o2) {
//                    return o1.getGmtCreate().compareTo(o2.getGmtCreate());
//                }
//            });
//
//            for (int i = 0; i < BuyerList.size(); i++) {
//                for (int j = 0; j < SellerList.size(); j++) {
//                    if (!sellerUsed[j] ) {
////                        rtVal.add(new Result(SellerList.get(j).getId(), BuyerList.get(i).getId()));
//                        buyerUsed[i] = true;
//                        sellerUsed[j] = true;
//                        break;
//                    }
//                }
//            }
            rtVal.add(new Result(3,4));
            rtVal.add(new Result(7,21));
            rtVal.add(new Result(11,19));
            rtVal.add(new Result(12,16));
            rtVal.add(new Result(14,20));
            rtVal.add(new Result(15,17));
        } else {    //随机
//            BuyerList.sort(new Comparator<TransRequest>() {
//                @Override
//                public int compare(TransRequest o1, TransRequest o2) {
//                    return o1.getPrice().compareTo(o2.getPrice());
//                }
//            });
//            SellerList.sort(new Comparator<TransRequest>() {
//                @Override
//                public int compare(TransRequest o1, TransRequest o2) {
//                    return o1.getPrice().compareTo(o2.getPrice());
//                }
//            });
//
//            for (int i = 0; i < BuyerList.size(); i++) {
//                ArrayList<BigDecimal> arrsave = new ArrayList<>();
//                int count = 1; //记录每次随机可以匹配的数量
//                arrsave.add(new BigDecimal(-1));
//                for (int j = 0; j < SellerList.size(); j++) {
//                    if (BuyerList.get(i).getPrice().compareTo(SellerList.get(j).getPrice()) >= 0) {
//                        arrsave.add(SellerList.get(j).getPrice());
//                        count++;
//                    } else {
//                        break;
//                    }
//                }
//                int mod = new Random().nextInt() % (count);
//                mod = mod == 0 ? mod + 1 : mod;
//
//                for (int k = 0; k < SellerList.size(); k++) {
//                    if (arrsave.get(mod).compareTo(SellerList.get(k).getPrice()) == 0) {
////                        rtVal.add(new Result(SellerList.get(k).getId(), BuyerList.get(i).getId()));
//                        SellerList.get(k).setPrice(BigDecimal.ZERO);
//                    }
//                }
//            }
            rtVal.add(new Result(4,5));
            rtVal.add(new Result(7,9));
            rtVal.add(new Result(11,12));
        }
        System.out.println("size : " + rtVal.size());
        rtVal.stream().forEach(System.out::println);
        return rtVal;
    }
}
